package cn.cqs.util;

import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;

import java.beans.PropertyDescriptor;
import java.util.HashSet;
import java.util.Set;

/**
 * @discription :解决SpringJPA更改所有字段
 * @author: cqs
 * @date: 2020-09-14 12:58
 */
public class JPASaveUtil {
    /**
     * 功能描述 :判断Entity属性不能null值的
     *
     * @param entity:
     * @return java.lang.String[]
     * @author cqs
     * @date 2020/9/14
     */

    public static String[] getNotNUllPropertyNames(Object entity) {
        BeanWrapper beanWrapper = new BeanWrapperImpl(entity);
        PropertyDescriptor[] propertyDescriptors = beanWrapper.getPropertyDescriptors();
        Set<String> set = new HashSet<String>();
        for (PropertyDescriptor pd : propertyDescriptors) {
            Object propertyValue = beanWrapper.getPropertyValue(pd.getName());
            if (propertyValue != null) {
                set.add(pd.getName());
            }
        }
//        set类型转数组set.toArray(T[])
        return set.toArray(new String[set.size()]);

    }

}
